<template>
  <div class="app-container">
    <el-row>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm el-form-bottom">
        <div>
          <span>文章管理添加</span>
          <el-divider></el-divider>
        </div>
        <el-col :span="8">
          <el-form-item label="标题" prop="title">
            <el-input v-model="ruleForm.title"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="文章分类" prop="typeId">
            <el-select v-model="ruleForm.typeId" placeholder="请选择" style="width: 323.06px">
              <el-option v-for="item in newsType" :label="item.name" :key="item.id" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="ruleForm.sort" :min="0" :max="99" label=""></el-input-number>
            <span class="form-word-hint">序号越小越靠前显示 最大99</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否显示" prop="status">
            <el-radio-group v-model="ruleForm.status">
              <el-radio v-for="item in statesList" :key="item.value" :label="item.value">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否推荐" prop="isRecommend">
            <el-radio-group v-model="ruleForm.isRecommend">
              <el-radio v-for="item in statesList" :key="item.value" :label="item.value">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="内容" prop="content">
            <editor v-model="ruleForm.content" :height="300" />
          </el-form-item>
        </el-col>
        <el-form-item>
          <div class="fixed-bottom-20">
            <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
            <el-button type="success" @click="back">返回</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>
<script>
import { getCodeManager, codeToArray } from "@/api/all";
import { tocreateNews, createNews } from "@/api/news";
import { Message } from "element-ui";
import Editor from "@/components/Editor";
export default {
  name: "noticeadd",
  components: {
    Editor,
  },
  data() {
    return {
      statesList: [],
      newsType: [],
      ruleForm: {
        title: "",
        typeId: "",
        status: "",
        isRecommend: "",
        sort: "",
        content: "",
      },
      rules: {
        title: [
          {
            required: true,
            message: "请输入标题",
            trigger: "blur",
          },
          {
            max: 20,
            message: "不能超过20个字符",
            trigger: "blur",
          },
        ],
        typeId: [
          {
            required: true,
            message: "请选择文章分类",
            trigger: "change",
          },
        ],
        status: [
          {
            required: true,
            message: "是否显示必选",
            trigger: "change",
          },
        ],
        isRecommend: [
          {
            required: true,
            message: "是否推荐必选",
            trigger: "change",
          },
        ],
        sort: [
          {
            required: true,
            message: "请输入权重",
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    getCodeManager("YES_NO", () => {
      this.statesList = codeToArray("YES_NO");
    });
    tocreateNews().then((res) => {
      this.newsType = res.data.data.typelist;
    });
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          createNews(this.ruleForm).then((res) => {
            if (!res.data.success) {
              Message.error(res.data.message);
              return;
            }
            Message.success("操作成功");
            setTimeout(() => {
              history.back();
            }, 1000);
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    back() {
      history.back();
    },
  },
};
</script>
<style scoped>
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

/deep/ [class*="el-col-"] {
  float: none;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  display: block;
  height: 150px;
}
</style>
